package io.xxx.mario.task.job;

import io.xxx.mario.task.ExecutionMode;
import io.xxx.mario.task.Task;
import io.xxx.mario.task.TaskMapper;
import io.xxx.mario.task.TaskService;
import jakarta.annotation.Resource;
import org.quartz.JobExecutionContext;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TaskInitializationJob extends QuartzJobBean {

    @Resource
    private TaskMapper taskMapper;

    @Resource
    private TaskService taskService;

    @Override
    protected void executeInternal(@NonNull JobExecutionContext context) {
        // 1. 查找需要初始化的任务
        List<Task> arrivedTasks = taskMapper.findArrivedTasks(ExecutionMode.ONCE.ordinal());
        // 2. 写入TaskReceiver关系表
        arrivedTasks.parallelStream()
                .forEach(task -> taskService.initialize(task));
    }
}
